package io.xccit.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author Xccit
 * @Date 2022-12-31
 * @Description SqlSession工具类
 */
public class SqlSessionUtil {
    private static InputStream inputStream;
    private static SqlSessionFactory sqlSessionFactory;
    private static SqlSession session;
    public static SqlSession getSession() {
        String resource = "sqlSessionConfig.xml";
        inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            session = sqlSessionFactory.openSession();
            return session;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static SqlSession getCommitSession() {
        String resource = "sqlSessionConfig.xml";
        inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            session = sqlSessionFactory.openSession(true);
            return session;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
